import unittest
import requests
from api.login import loginAPI

class Testlogin(unittest.TestCase):
    def setUp(self):
        self.login_api = loginAPI()
        #self.session = requests.Session()

    #def tearDown(self):
        #if self.session:
            #self.session.close()
    #登录成功
    def test01(self):
        port = self.login_api.login({"mobile":"13800000002","password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(True, port.json().get("success"))
        self.assertEqual(10000, port.json().get("code"))
        self.assertIn("操作成功", port.json().get("message"))

    #不输入手机号
    def test02(self):
        port = self.login_api.login({"mobile":"","password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #不输入密码
    def test03(self):
        port = self.login_api.login({"mobile":"13800000002","password":""})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #手机长度小于十一位
    def test04(self):
        port = self.login_api.login({"mobile":"1380000000","password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #手机长度大于十一位
    def test05(self):
        port = self.login_api.login({"mobile":"138000000021","password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #手机号为非数字
    def test06(self):
        port = self.login_api.login({"mobile":"error","password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #未注册手机号
    def test07(self):
        port = self.login_api.login({"mobile":"17633863035","password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #多参
    def test08(self):
        port = self.login_api.login({"mobile":"13800000002","password":"929itheima.CN032@.20250220", "aa":"bb"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(True, port.json().get("success"))
        self.assertEqual(10000, port.json().get("code"))
        self.assertIn("操作成功", port.json().get("message"))

    #缺少手机号
    def test09(self):
        port = self.login_api.login({"password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))


    #缺少密码
    def test010(self):
        port = self.login_api.login({"mobile":"13800000002"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #无参
    def test011(self):
        port = self.login_api.login(None)
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(99999, port.json().get("code"))
        self.assertIn("抱歉，系统繁忙", port.json().get("message"))

    #错误参数-账号
    def test012(self):
        port = self.login_api.login({"mobila":"13800000002","password":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))

    #错误参数-密码
    def test013(self):
        port = self.login_api.login({"mobile":"13800000002","passwords":"929itheima.CN032@.20250220"})
        print(port.json())
        self.assertEqual(200, port.status_code)
        self.assertEqual(False, port.json().get("success"))
        self.assertEqual(20001, port.json().get("code"))
        self.assertIn("用户名或密码错误", port.json().get("message"))
